package com.sgb.kjwl.view.ui.adapter.shopping;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.shopping.ShoppingCarResponse;
import com.sgb.kjwl.utils.DisplayUtil;
import com.swgk.core.util.UiUtil;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;


import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 作者:张磊
 * 日期:2021/3/17
 * 说明:采购单item父级
 *
 * @author Administrator
 */
public class ShopParentAdapter extends BaseQuickAdapter<ShoppingCarResponse, BaseViewHolder> {

    private Drawable drawableVip;
    private int drawablePadding;
    private final String COMPANY_ON = "-1";

    public ShopParentAdapter(Context context, List<ShoppingCarResponse> beans) {
        super(R.layout.item_parent, beans);
        initVipDrawable(context);
    }

    private void initVipDrawable(Context context) {
        this.drawableVip = ContextCompat.getDrawable(context, R.drawable.btn_right_arrow);
        if (this.drawableVip != null) {
            this.drawableVip.setBounds(0, 0, DisplayUtil.convertDpToPixel(context, 9), DisplayUtil.convertDpToPixel(context, 15));
        }
        this.drawablePadding = DisplayUtil.convertDpToPixel(context, 10);
    }

    @Override
    protected void convert(BaseViewHolder helper, final ShoppingCarResponse item) {
        final boolean[] isChecked = new boolean[1];
        if ("1".equals(item.getSelfSupportTag())) {
            helper.setVisible(R.id.image_view, true);
        } else {
            helper.setVisible(R.id.image_view, false);
        }
        helper.setText(R.id.tv_store_name, item.getCompanyName());
        RecyclerView rv = helper.getView(R.id.rv);
        isChecked[0] = item.isChecked();
        helper.setImageResource(R.id.iv_select, isChecked[0] ? R.drawable.icon_select_item : R.drawable.icon_no_select_item);
        rv.setLayoutManager(new LinearLayoutManager(UiUtil.getContext()));
        List<ShoppingCarResponse.ItemsData> dataBeans = item.getItems();
        ShopChildAdapter adapterChild = new ShopChildAdapter(dataBeans);

        for (ShoppingCarResponse.ItemsData dataBean : adapterChild.getData()) {
            dataBean.setChecked(isChecked[0]);
        }

        rv.setAdapter(adapterChild);
        TextView tvCompany = helper.getView(R.id.tv_store_name);

        if (!COMPANY_ON.equals(item.getCompanyNo())) {
            helper.getView(R.id.iv_select).setVisibility(View.VISIBLE);
            showCompanyIcon(tvCompany, true);
            helper.getView(R.id.shop_name_ll).setOnClickListener(v -> {
                if (TextUtils.isEmpty(String.valueOf(item.getCompanyNo()))) {
                    ToastUtil.show(mContext, "企业店铺No不能为空");
                    return;
                }
                WebsiteInfoActivity.start(mContext, item.getCompanyNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY, 0, 0);
            });
        } else {
            helper.getView(R.id.iv_select).setVisibility(View.INVISIBLE);
            showCompanyIcon(tvCompany, false);
            helper.getView(R.id.shop_name_ll).setOnClickListener(null);
        }

        adapterChild.setOnSelectListener(b -> {
//            isChecked[0] = !b;
            if (isItemAllChecked(adapterChild)) {
                item.setChecked(true);
                helper.setImageResource(R.id.iv_select, R.drawable.icon_select_item);
            } else {
                item.setChecked(false);
                helper.setImageResource(R.id.iv_select, R.drawable.icon_no_select_item);
            }
            UiUtil.sendMsg(EventConstant.EVENT_ITEM_SELSECT, null);
        });

        helper.getView(R.id.ll_sel).setOnClickListener(v -> {
            isChecked[0] = !isChecked[0];
            item.setChecked(isChecked[0]);
            helper.setImageResource(R.id.iv_select, isChecked[0] ? R.drawable.icon_select_item : R.drawable.icon_no_select_item);
            for (ShoppingCarResponse.ItemsData dataBean : adapterChild.getData()) {
                dataBean.setChecked(isChecked[0]);
            }
//            if (isAllChecked(getData())) {
//                UiUtil.sendMsg(EventConstant.EVENT_ALL_SELSECT, null);
//            } else {
//                UiUtil.sendMsg(EventConstant.EVENT_CANCEL_ALL_SELSECT, null);
//            }
            UiUtil.sendMsg(EventConstant.EVENT_ITEM_SELSECT, null);
            adapterChild.notifyDataSetChanged();
        });
    }

    private boolean isItemAllChecked(ShopChildAdapter adapterChild) {
        if (adapterChild != null && adapterChild.getData().size() != 0) {
            for (ShoppingCarResponse.ItemsData dataBean : adapterChild.getData()) {
                if (!dataBean.isChecked()) {
                    return false;
                }
            }
            return true;
        }

        return false;
    }

    private void showCompanyIcon(TextView tvCompany, boolean isVisibility) {
        if (isVisibility) {
            tvCompany.setCompoundDrawables(null, null, drawableVip, null);
            tvCompany.setCompoundDrawablePadding(drawablePadding);
        } else {
            tvCompany.setCompoundDrawables(null, null, null, null);
        }
    }
}
